package Demo0;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class FileTest {
    public static void main(String[] args) throws IOException {
        String filePath = "D:/MyFolder";
        String fileName = "test.txt";
        File myFolder = new File(filePath);
        if (!myFolder.exists()) {
            myFolder.mkdirs();
        }

        File myFile = new File(filePath + "/" + fileName);
        if (!myFile.exists()) {
            myFile.createNewFile();
        }
        System.out.println("------------------------------------------");
        System.out.println("文件绝对路径:" + myFile.getAbsolutePath());
        System.out.println("文件名称:" + myFile.getName());
        System.out.println("文件父节点:" + myFile.getParent());
        System.out.println("是文件夹吗？:" + myFile.isDirectory());
        System.out.println("是文件吗？:" + myFile.isFile());
        System.out.println("是隐藏的吗？:" + myFile.isHidden());
        System.out.println("是可读的吗？:" + myFile.canRead());
        System.out.println("是可写的吗？:" + myFile.canWrite());
        System.out.println("是可执行的吗？:" + myFile.canExecute());
        System.out.println("最后一次修改时间：" + new Date(myFile.lastModified()));
        System.out.println("文件的大小:" + myFile.length() + "字节");
        System.out.println("------------------------");
        File[] allFileInPath = myFolder.listFiles();
        System.out.println("文件：");
        for (File file : allFileInPath) {
            if (file.isFile()) {
                System.out.println("\t" + file.getName());
            }
        }
        System.out.println("文件夹：");
        for (File file : allFileInPath) {
            if(file.isDirectory()){
                System.out.println("\t" + file.getName());
            }
        }
        File[] roots = File.listRoots();//取得所有的根，如果是windows系统那么将取得所有的磁盘
        for(int i=0;i<roots.length;i++){
            System.out.println(roots[i]);
            System.out.println("空闲空间 " + roots[i].getFreeSpace());
            System.out.println("可用空间 " + roots[i].getUsableSpace());
            System.out.println("总空间" + roots[i].getTotalSpace());
            System.out.println();
        }
        System.out.println("----------------------------------------------");
        myFile.delete();
        if (!myFile.exists()) {
            System.out.println("文件删除成功");
        }
        myFolder.delete();
        if (!myFolder.exists()) {
            System.out.println("文件夹删除成功");
        }
       System.out.println("-------------------------------------------------");

    }
}

